package suanfa;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class test6 {

    public boolean isValid(String s) {
        Map<Character, Character> map = new HashMap<Character, Character>();//{[()]}
        map.put(')', '(');
        map.put('}', '{');
        map.put(']', '[');
        Stack<Character> stack = new Stack<>();
        if (s.length() == 1) return false;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsValue(s.charAt(i)))
                stack.push(s.charAt(i));
            if (map.containsKey(s.charAt(i))) {
                if (!stack.empty()) {
                    if (map.get(s.charAt(i)) == stack.peek()) {
                        stack.pop();
                    } else {
                        stack.push(s.charAt(i));
                    }

                } else
                    return false;

            }


        }
        if (stack.size() != 0)
            return false;
        else
            return true;
    }
}
